// Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
// This source file is part of the Cangjie project, licensed under Apache-2.0
// with Runtime Library Exception.
//
// See https://cangjie-lang.cn/pages/LICENSE for license information.

#ifndef CANGJIE_MIGRATOR_RULES_RULE
#define CANGJIE_MIGRATOR_RULES_RULE

#include <memory>
#include <string>
#include <vector>

#include "Actions/Action.h"
#include "TomlFile.h"
#include "UpdateInstance.h"
#include "Rules/ActionRule.h"
#include "Rules/MatchRule.h"
#include "Rules/ImportRule.h"

#define GET_VALUE_IF_EMPTY_RETURN_NULL(config, element) \
    std::string element;                                \
    do {                                                \
        element = config[#element];                     \
        if (element.empty()) {                          \
            return nullptr;                             \
        }                                               \
    } while (false)

namespace CangjieMigrator::Rules {

class Rule {
public:
    Rule(std::string reference, std::unique_ptr<MatchRule> matchRule, ActionRule actionRule, ImportRule importRule);

    static std::unique_ptr<Rule> TryCreate(const TomlConfig &config);

    std::vector<Actions::Action> GenerateAction(UpdateInstance::File &file, const ExtractedInfo &info) const;

    const std::string reference;

    std::unique_ptr<MatchRule> matchRule;
    ImportRule importRule;

private:
    ActionRule actionRule;
};

} // namespace CangjieMigrator::Rules

#endif // CANGJIE_MIGRATOR_RULES_RULE